<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/23
 * Time: 15:13
 */
namespace app\index\controller;

use function Sodium\compare;
use think\Db;

class Teacount extends Common
{
	//判断是否登录
	public function _initialize()
	{
		parent::_initialize();
	}
	/**
	 * 统计首页
	*/
	public function index()
	{
		$sid = input('get.sid');
		return view('index',compact('sid'));
	}
	
	/**
	 * 统计信息
	 * @param string get.sid 店id
	 * @param string get.rows 每页显示的条数
	 * @param string get.page 第几页
	 * @param string get.title 要检索的字段
	 * @param string get.date 要检索的时间
	*/
	public function info()
	{
		//检索的条件
		$wherelike = [];
		//开始时间
		$wherestart = [];
		//结束时间
		$whereend = [];
		$date = input('get.date');
		$sid = input('get.sid');
		$rows = input('get.rows');
		$page = input('get.page');
		if(!empty(input('get.title'))){
			$title = input('get.title');
			$wherelike['tea.name'] = ['like',"%{$title}%"];
		}
		if(!empty($date)) {
			$data = explode('-', $date);
			$info = getStartAndEndUnixTimestamp($data[0], $data[1], $data[2]);
			$wherestart['selltea.time'] = ['>', $info['start']];
			$whereend['selltea.time'] = ['<', $info['end']];
		}
//		}else{
//			$data = explode('-',date('Y-m-d',time()));
//			$info = getStartAndEndUnixTimestamp($data[0],$data[1],$data[2]);
//			$wherestart['selltea.time'] = ['>',$info['start']];
//			$whereend['selltea.time'] = ['<',$info['end']];
//		}
		//结算总条数
		$total = Db::table('selltea')
			->join('tea','tea.id = selltea.teaid','LEFT')
			->whereOr($wherelike)->where($wherestart)->where($whereend)
			->group('tea.name,selltea.type,selltea.ordernumber')->where('selltea.status',1)
			->where('selltea.sid',$sid)->count();
		//查询信息
		$rows = Db::table('selltea')->field('tea.name,selltea.number as number,selltea.teaid,selltea.id,selltea.type,selltea.time')
			->join('tea','tea.id = selltea.teaid','LEFT')
			->whereOr($wherelike)->where($wherestart)->where($whereend)->where('selltea.sid',$sid)
			->group('tea.name,selltea.type,selltea.ordernumber')->where('selltea.status',1)
			->limit($rows)->page($page)->select();
		//分页时需要获取记录总数，键值为 total
		$result["total"] = $total;
		//获取的记录
		$result["rows"] = $rows;
		return json($result);
	}
	/**
	 * 详情
	 * @param string get.teaid 茶表id
	 * @param string get.sid 店id
	*/
	public function detail()
	{
		$teaId = input('get.teaid');
		$sid = input('get.sid');
		return view('detail',compact('teaId','sid'));
	}
	
	/**
	 * @param string get.teaid 茶表id
	 * @param string get.sid 店id
	 * @param string get.rows 每页显示的条数
	 * @param string get.page 第几页
	 * @param string get.title 要检索的字段
	 * @param string get.date 要检索的时间
	*/
	public function detailInfo()
	{
		$sid = input('get.sid');
		$teaid = input('get.teaid');
		$rows = input('get.rows');
		$page = input('get.page');
		//检索的条件
		$wherelike = [];
		//开始时间
		$wherestart = [];
		//结束时间
		$whereend = [];
		$total = Db::table('selltea')
			->join('tea','tea.id = selltea.teaid','LEFT')
			->whereOr($wherelike)->where($wherestart)->where($whereend)
			->group('selltea.type')->where('selltea.status',1)
			->where('selltea.sid',$sid)
			->where('selltea.teaid',$teaid)
			->count();
		//查询信息
		$rows = Db::table('selltea')->field('tea.name,selltea.number,selltea.time,selltea.type')
			->join('tea','tea.id = selltea.teaid','LEFT')
			->whereOr($wherelike)->where($wherestart)->where($whereend)
			->where('selltea.sid',$sid)->where('selltea.teaid',$teaid)->group('selltea.type')
			->where('selltea.status',1)->order("selltea.id desc")
			->limit($rows)->page($page)->select();
		//分页时需要获取记录总数，键值为 total
		$result["total"] = $total;
		//获取的记录
		$result["rows"] = $rows;
		return json($result);
	}
}